package de.baumann.browser.present.gg

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.gg.ExtractTrans
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.gg.IRansomView
import de.baumann.browser.model.*
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import java.math.BigDecimal
import java.security.PrivateKey

class RansomPresenter : BasePresenter<IRansomView>() {

    fun b01() {
        userDepositsNum(view!!.getSmelterAddress(), view!!.getUserAddress())
                .subscribe(object : Observer<BigDecimal> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: BigDecimal) {
                        view?.setMortgageNum(t)
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }
                })
    }

    fun b011() {
        ownerUserCounts(view!!.getSmelterAddress())
                .subscribe(object : Observer<BigDecimal> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: BigDecimal) {
                        view?.setUserCount(t)
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }
                })
    }


    fun b02(privateKey: String, amount: String) {
        userWithdraw(view!!.getUserAddress(), amount, privateKey)
                .subscribe(object : Observer<String> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: String) {
                        b03(amount, t)
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }

                })
    }

    fun b021(privateKey: String, amount: String) {
        ownerWithdraw(view!!.getUserAddress(), privateKey)
                .subscribe(object : Observer<String> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: String) {
                        b03(amount, t)
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }

                })
    }

    fun b03(amount: String, hash: String) {
        redemption(view!!.getUserAddress(), amount, hash)
                .subscribe(object : OdinObserver<Any>() {
                    override fun onSuccess(data: Any?) {
                        view?.ransomSuccess()
                    }
                })
    }

    fun b04() {
        getExtractTrans(view!!.getUserAddress())
                .subscribe(object : OdinObserver<List<ExtractTrans>>() {
                    override fun onSuccess(data: List<ExtractTrans>?) {
                        if (data != null) {
                            view?.setExtracts(data)
                        }
                    }
                })
    }
}